package com.cjk.console.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cjk.foundation.commons.response.Response;
import com.cjk.foundation.dao.file.AdDao;
import com.cjk.foundation.dao.file.FileModuleDao;
import com.cjk.foundation.enums.ConfStatus;
import com.cjk.foundation.enums.IsStatus;
import com.cjk.foundation.request.WebUploadRequest;
import com.cjk.foundation.util.FileUtil;

@Service
public class FileServiceImpl implements FileService {
	
	@Autowired
	private AdDao adDao;
	
	@Autowired
	private FileModuleDao fileModuleDao;

	@Transactional
	@Override
	public Response upload(WebUploadRequest request, Integer userId) {
		final Integer groupId = request.getGroupId();
		final String dir = "/easyTv/data/user_" + userId + "/group_" + groupId + "/" + request.getPath();
		
		FileUtil.storeFile(request.getFile(), dir, request.getName());
		
		fileModuleDao.updateStateByModule("hotelZip", groupId, IsStatus.TRUE);
		
		adDao.insert(groupId, userId, ConfStatus.NOT_SYNC);
		
		return Response.success();
	}

}
